<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>canvas</title>
    <style>
    #canvas{
        display: block;
        margin: 0 auto;
        border: 1px solid #eee;
    }
    </style>
</head>
<body>
    <canvas id="canvas" width="400" height="300"></canvas>
    <script>
        /** @type {HTMLCanvasElement} */
        let canvas = document.querySelector('#canvas');
        let ctx = canvas.getContext('2d');
        // 创建线性渐变
        let linear = ctx.createLinearGradient(0,0,400,300);
        // 添加过渡颜色
        linear.addColorStop(0,'red')
        linear.addColorStop(0.5,'green')
        linear.addColorStop(1,'blue')
        // 填充渐变
        ctx.fillStyle = linear;
        ctx.fillRect(50,50,100,100)
    </script>
</body>
</html>